﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Globalization;
using System.Drawing;

namespace GPS.Controllers.GPS_TheoDoiBienDong
{
    public class GPS_TheoDoiBienDongPhuongTienController : Controller
    {
        //
        // GET: /GPS_TheoDoiBienDongGiaoVien/
        #region unit
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        public void initialCategorySearchAction(ViewModelTDBD_PHUONGTIEN viewModel)
        {
            ViewBag.checkuser = this.getCurrentUser().MA_TINHTP;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, viewModel.pPT_DVGTVT_MA);
            viewModel.pPT_DVGTVT_MA = getdvgtvt_Ma;
        }
        #endregion

        #region index
        public ActionResult Index(ViewModelTDBD_PHUONGTIEN SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            List<CrudModelTDBD_PHUONGTIEN> model = GetListData(SearchModel);
            int icount = 1;
            foreach (var item in model)
            {
                item.STT = icount.ToString();
                icount++;
            }
            ViewBag.TotalRow = model == null ? "0" : model.Count().ToString();
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }
        #endregion

        public List<CrudModelTDBD_PHUONGTIEN> GetListData(ViewModelTDBD_PHUONGTIEN SearchModel)
        {
            var _pPT_DVGTVT_MA = new OracleParameter("pPT_DVGTVT_MA", OracleDbType.Varchar2, (SearchModel.pPT_DVGTVT_MA == null ? DBNull.Value : (object)SearchModel.pPT_DVGTVT_MA), ParameterDirection.Input);
            var _pPT_BIENSO = new OracleParameter("pPT_BIENSO", OracleDbType.Varchar2, (SearchModel.pPT_BIENSO == null ? DBNull.Value : (object)SearchModel.pPT_BIENSO.Trim()), ParameterDirection.Input);
            var _pRET = new OracleParameter("pRET", OracleDbType.RefCursor, ParameterDirection.Output);
            var entities = _db.DbContext.Database.SqlQuery<CrudModelTDBD_PHUONGTIEN>("BEGIN GPS_THEODOI.GPS_TDBD_PHUONGTIEN(:pPT_DVGTVT_MA,:pPT_BIENSO,:pRET); end;"
                                                        , _pPT_DVGTVT_MA
                                                        , _pPT_BIENSO
                                                        , _pRET).ToList();
            return entities;
        }
    }
}